package com.honghe.system.controller;

import com.honghe.common.annotation.Log;
import com.honghe.common.core.controller.BaseController;
import com.honghe.common.core.domain.AjaxResult;
import com.honghe.common.core.page.TableDataInfo;
import com.honghe.common.enums.BusinessType;
import com.honghe.common.utils.SecurityUtils;
import com.honghe.common.utils.poi.ExcelUtil;
import com.honghe.system.domain.BankCustomerCases;
import com.honghe.system.domain.BankCustomerCasesStatistic;
import com.honghe.system.service.IStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/system/statistic")
public class StatisticController extends BaseController {
    @Autowired
    private IStatisticService statisticService;

    /**
     * 查询有案号的客户统计列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BankCustomerCasesStatistic statistic) {
        startPage();
        List<BankCustomerCasesStatistic> list = statisticService.selectCustomerCasesStatisticList(statistic);
        return getDataTable(list);
    }
    
    /**
     * 从原表同步数据到统计表
     */
    @Log(title = "客户案号统计-同步数据", businessType = BusinessType.OTHER)
    @PostMapping("/sync")
    public AjaxResult syncData() {
        // 获取当前登录用户名
        String username = SecurityUtils.getUsername();
        int rows = statisticService.syncCustomerCasesStatistic(username);
        return AjaxResult.success("同步成功，共同步 " + rows + " 条数据");
    }
    
    /**
     * 导出有案号的客户统计列表
     */
    @Log(title = "客户案号统计", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(BankCustomerCasesStatistic statistic, javax.servlet.http.HttpServletResponse response) {
        // 先获取统计表数据
        List<BankCustomerCasesStatistic> list = statisticService.selectCustomerCasesStatisticList(statistic);
        // 如果统计表没有数据，先自动同步一次
        if (list == null || list.isEmpty()) {
            logger.info("导出前发现统计表无数据，自动执行同步操作");
            String username = SecurityUtils.getUsername();
            statisticService.syncCustomerCasesStatistic(username);
            // 同步后再次查询数据
            list = statisticService.selectCustomerCasesStatisticList(statistic);
        }
        // 如果仍然没有数据，改为从原表直接查询导出
        if (list == null || list.isEmpty()) {
            logger.info("统计表同步后仍无数据，尝试从原表直接查询导出");
            BankCustomerCases param = new BankCustomerCases();
            // 转换查询参数
            if (statistic != null) {
                param.setCustomerName(statistic.getCustomerName());
                param.setIdCardNumber(statistic.getIdCardNumber());
                param.setLitigationMediationCaseNumber(statistic.getLitigationMediationCaseNumber());
                param.setEnforcementCaseNumber(statistic.getEnforcementCaseNumber());
            }
            // 从原表查询有案号的客户
            List<BankCustomerCases> originalList = statisticService.selectStatisticCustomerCasesList(param);
            // 将原表数据转换为统计表格式
            if (originalList != null && !originalList.isEmpty()) {
                list = originalList.stream().map(item -> {
                    BankCustomerCasesStatistic s = new BankCustomerCasesStatistic();
                    s.setCaseId(item.getCaseId());
                    s.setCustomerName(item.getCustomerName());
                    s.setIdCardNumber(item.getIdCardNumber());
                    s.setLitigationMediationCaseNumber(item.getLitigationMediationCaseNumber());
                    s.setEnforcementCaseNumber(item.getEnforcementCaseNumber());
                    return s;
                }).collect(java.util.stream.Collectors.toList());
            }
        }
        ExcelUtil<BankCustomerCasesStatistic> util = new ExcelUtil<>(BankCustomerCasesStatistic.class);
        util.exportExcel(response, list, "客户案号统计数据");
    }
    
    /**
     * 获取客户案号统计详情
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(statisticService.selectCustomerCasesStatisticById(id));
    }
} 